<?php
/**
 * Hook handler for standard TYPO3 contact form
 *
 * @author Sven Burkert <sventb@googlemail.com>
 */

class tx_cssspamprotect_mailform {
	private $extKey = 'cssspamprotect';

	/**
	 * Called before sending contact email
	 * Check for spam
	 *
	 * @param array $EMAIL_VARS: User input
	 * @param object(tslib_fe) $parentObj
	 * @return array
	 */
	function sendFormmail_preProcessVariables($EMAIL_VARS, $parentObj) {
		$conf = $GLOBALS['TSFE']->tmpl->setup['plugin.']['cssspamprotect.'];
		// read in TS Setup if it's not available in TSFE (perhaps because site is cached?)
		if(!$conf) {
			$tsSetup = $this->loadTS($GLOBALS['TSFE']->id);
			$conf = $tsSetup['plugin.']['cssspamprotect.'];
		}
		if(array_key_exists('fieldName', $conf) && array_key_exists($conf['fieldName'], $EMAIL_VARS) && $EMAIL_VARS[$conf['fieldName']]) {
			if(array_key_exists('redirectPid', $conf) && $conf['redirectPid']) {
				header('Location:' . t3lib_div::getIndpEnv('TYPO3_SITE_URL') . '?id=' . $conf['redirectPid']);
				exit;
			} else {
				die('ERROR: Your message is defined as spam and therefore it was not submitted.');
			}
		} else {
			unset($EMAIL_VARS[$conf['fieldName']]);
			return $EMAIL_VARS;
		}
	}

	/**
	 * Get TypoScript configuration for a specific page
	 *
	 * @param integer $pageId: Page id
	 * @return array: TS Setup
	 */
	function loadTS($pageId) {
		$pS = t3lib_div::makeInstance('t3lib_pageSelect');
		tslib_fe::includeTCA();
		$TSObj = t3lib_div::makeInstance('t3lib_TStemplate');
		$TSObj->tt_track = 0;
		$TSObj->init();
		$TSObj->start($pS->getRootline((int)$pageId));
		return $TSObj->setup;
	}
}
?>